clear all
global system "linux"

if "${system}" == "linux" {
	global code "/"
	global s "/"
}

run "${code}${s}_set-path.do"


args index
cap log close _all
log using "${log}/log-harp-`index'", name("log`index'") text replace

local MaxVar = 2
local MaxAge = 4
local MaxSample = 2

local VarType = ceil(`index'/ `MaxAge'/ `MaxSample')
if `VarType' == 1 local Var "pp"
if `VarType' == 2 local Var "forcl"

local index1 = `index' - (`VarType' - 1)* `MaxAge'* `MaxSample'
local AgeType = ceil(`index1'/ `MaxSample')
local Age = 12 + `AgeType'* 12

local index2 = `index1' - (`AgeType' - 1)* `MaxSample'
local SampleType = `index2'

local VarAge "`Var'`Age'"
di "`VarAge'"

*** Load data
use "${temp}/gse_id-harp-cleaned.dta", clear

do _HARP-sampleselection // sample selection

** Create necesary variables
local Runvar "ltvprev_diff"
gen Cutoff = hi_ltvprev


keep if ltvprev >= 95 & ltvprev <= 115
if `SampleType'==2 {
	su credit_scorecore_b, d
	keep if credit_scorecore_b <= `r(p50)'
}

** Merge performance data
merge 1:1 id_loan_harp using "${cached}/harp-performance-combined.dta", keep(matched master)
keep if _merge==3
drop _merge

** Filling in Prepaid for loans that could have existed until age n. 

gen max_age = max_ym - orig_ym
foreach n of numlist 12(12)84 {
	rename current_upb`n' curr_rpb`n'
	replace curr_rpb`n' = 0 if curr_rpb`n' == . & prepaid == 1 & last_age+1 <= `n'  & max_age > `n'+1
	replace curr_rpb`n' = curr_rpb`n' / orig_amount

	gen pp`n' = (curr_rpb`n' == 0) if curr_rpb`n' ~= . 
	gen forcl`n' = (curr_rpb`n' == 0) & forcl==1 if curr_rpb`n' ~= . 
}


global poly1 "Cutoff c.`Runvar' c.`Runvar'#1.Cutoff"
global poly2 "$poly1 c.`Runvar'#c.`Runvar' c.`Runvar'#c.`Runvar'#1.Cutoff"
global poly3 "$poly2 c.`Runvar'#c.`Runvar'#c.`Runvar' c.`Runvar'#c.`Runvar'#c.`Runvar'#1.Cutoff"

*** Regression specification
do _HARP-spec
	
	
** rate x month FE
gen ratebin = floor(orig_rt/.25)
egen rate_ym = group(ratebin orig_ym)
	
** Plot

global Poly $poly3

reghdfe `VarAge' $Poly $controls, a($FEs FEextra = i.rate_ym) vce(robust)
estimate save "${Estimates}/harp-`VarAge'-Sample`SampleType'", replace

predict resid_`VarAge', r
drop FE* 
foreach x in $Poly {
	replace resid_`VarAge' = resid_`VarAge' + _b[`x']* (`x')
}

preserve
	replace ltvprev_diff = ceil(ltvprev_diff* 4)/4
	collapse resid_`VarAge', by(ltvprev_diff)

	qui do "${code}/_labels"
	
	twoway scatter resid_`VarAge' ltvprev_diff, xline(0)  ///
	 saving("${Estimates}/harp-`VarAge'-Sample`SampleType'.gph", replace) ///
	 title("Loan Age `Age'") ytitle("") xtitle("")  ylabel(, angle(0))
	
restore

cap log close _all
